//
//  CXHTopOnNativeExpressEvent.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/11.
//

#import "CXHTopOnNativeExpressEvent.h"
#import "CXHTopOnCommonBiddingManager.h"
#import "CXHTopOnCommonBiddingRequest.h"
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeExpressCallBack.h>
#import "CXHTopOnCommonAdapter.h"
#import "CXHTopOnNativeObject.h"

@interface CXHTopOnNativeExpressEvent ()

@property (nonatomic, strong) CXHAdSDKNativeExpressAd *nativeExpresAd;

@end

@implementation CXHTopOnNativeExpressEvent


- (void)nativeExpressAdLoadedWith:(CXHAdSDKNativeExpressAd *)nativeExpresAd nativeExpressAdView:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)nativeExpressAdView controller:(UIViewController *)viewController{
    
    [nativeExpressAdView cxh_setRootViewController:viewController];
    
    NSMutableArray<NSDictionary*>* assets = [NSMutableArray<NSDictionary*> array];
    NSMutableDictionary *asset = [NSMutableDictionary dictionary];
    
    [asset setValue:self forKey:kATAdAssetsCustomEventKey];
    [asset setValue:nativeExpressAdView forKey:kATAdAssetsCustomObjectKey];
    // 原生模板广告
    [asset setValue:@(1) forKey:kATNativeADAssetsIsExpressAdKey];
    [asset setValue:@(nativeExpressAdView.frame.size.width) forKey:kATNativeADAssetsNativeExpressAdViewWidthKey];
    [asset setValue:@(nativeExpressAdView.frame.size.height) forKey:kATNativeADAssetsNativeExpressAdViewHeightKey];
    
    [asset setValue:nativeExpresAd forKey:@"kCXHNativeExpressObj"];
    [assets addObject:asset];
    
    [self trackNativeAdLoaded:assets];
}

- (void)expressRendering:(CXHAdSDKNativeExpressAd *)nativeAd adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *> *)adViewArray{
    NSDictionary *infoDic = [CXHTopOnCommonAdapter getC2SInfo:[nativeAd getBidPrice]];
    
    NSMutableArray *offerArray = [NSMutableArray array];
    ATCustomNetworkNativeAd *native;
}

#pragma mark - CXHAdSDKNativeExpressAdDelegate

/**
 信息流广告请求成功

 @param nativeAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXH_nativeAdSucessToLoad:(CXHAdSDKNativeExpressAd *)nativeAd
                     adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *> *)adViewArray{
    NSDictionary *infoDic = [CXHTopOnCommonAdapter getC2SInfo:[nativeAd getBidPrice]];
    
    NSMutableArray *assets = [NSMutableArray array];
    
    for (UIView<CXHAdapterNativeExpressAdViewDelegate> *obj in adViewArray) {
        CXHTopOnNativeObject *nativeObj = [[CXHTopOnNativeObject alloc]init];
        nativeObj.nativeExpressAd = nativeAd;
        nativeObj.templateView = obj;
        nativeObj.nativeAdRenderType = ATNativeAdRenderExpress;
        nativeObj.nativeExpressAdViewWidth = obj.frame.size.width;
        nativeObj.nativeExpressAdViewHeight = obj.frame.size.height;
        nativeObj.isVideoContents = false;
        [assets addObject:nativeObj];
    }
    
    [self.adStatusBridge atOnNativeAdLoadedArray:assets adExtra:infoDic];
    
    self.nativeExpresAd = nativeAd;
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXH_nativeAdFailToLoad:(CXHAdSDKNativeExpressAd *)nativeAd
                         error:(NSError *)error{
    [self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}

/**
 信息流广告渲染成功

 @param adView 广告视图
 */
- (void)CXH_nativeAdViewRenderOrRegistSuccess:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"========CXH_nativeAdViewRenderOrRegistSuccess");
    
}

/**
 信息流广告渲染失败

 @param adView 广告视图
 */
- (void)CXH_nativeAdViewRenderOrRegistFail:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    NSError *err = [NSError errorWithDomain:@"com.cxh.nativeExpress"
                                       code:100
                                   userInfo:@{}];
    [self.adStatusBridge atOnAdShowFailed:err extra:nil];
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClicked:(CXHAdSDKNativeExpressAd *)nativeAd adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    [self.adStatusBridge atOnAdClick:nil];
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClose:(CXHAdSDKNativeExpressAd *)nativeAd adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    [self.adStatusBridge atOnAdClosed:nil];
}

/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposure:(CXHAdSDKNativeExpressAd *)nativeAd adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    [self.adStatusBridge atOnAdShow:nil];
}

/**
 信息流广告展示上报
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposureReport:(CXHAdSDKNativeExpressAd *)nativeAd
                            adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
}

/**
 信息流广告点击上报

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClickedReport:(CXHAdSDKNativeExpressAd *)nativeAd
                           adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
}

/**
 信息流广告落地页被关闭
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdCloseLandingPage:(CXHAdSDKNativeExpressAd *)nativeAd
                              adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    [self.adStatusBridge atOnAdDetailClosed:nil];
}

- (void)dealloc
{
    NSLog(@"------> %s", __FUNCTION__);
}

@end
